<template>
  <div class="address">
    <van-nav-bar
      title="新增收货地址信息"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-address-edit
      :area-list="areaList"
        :addressInfo="addressInfo"
        show-area
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>

<script>
import { addreceiptapi } from "../../api/receipt";
import areaList from "../../util/area";
import { Toast } from "vant";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addressInfo:{
        areaCode:"",
      }
    };
  },
  computed: {},
  watch: {},

  methods: {
    //返回上一步
    onClickLeft() {
      Toast("返回");
      this.$router.go(-1);
    },
    async onSave(obj) {
      console.log(obj);
      const receiver = obj.name;
      const regions = obj.province + "/" + obj.city + "/" + obj.county+"/"+obj.areaCode;
      const address = obj.addressDetail;
      const mobile = obj.tel;
      const idDefault = obj.isDefault;

      localStorage.setItem("receiver", receiver);
      localStorage.setItem("regions", regions);
      localStorage.setItem("address", address);
      localStorage.setItem("mobile", mobile);
      localStorage.setItem("idDefault", idDefault);
      this.$store.commit("address/changereceiver", receiver);
      this.$store.commit("address/changeregions", regions);
      this.$store.commit("address/changeaddress", address);
      this.$store.commit("address/changemobile", mobile);
      this.$store.commit("address/changeidDefault", idDefault);
      const result = await addreceiptapi({receiver,regions,address,mobile,idDefault});
      if (result.code == "success") {
        Toast("已新增地址信息");
         this.$router.go(-1);
      }
    },
  },
  created() {},
  mounted() {},
  components: {},
};
</script>
<style scoped></style>
